package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.QueryBaseParam;
import com.example.demo.domain.entity.TrainHomework;
import com.example.demo.enums.HomeWorkStatus;
import com.example.demo.enums.HomeWorkTypeEnum;
import com.example.demo.param.TrainClassUserParam;
import com.example.demo.param.TrainHomeworkParam;
import com.example.demo.service.TrainHomeworkService;
import com.example.demo.mapper.TrainHomeworkMapper;
import com.example.demo.utils.CommonUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author DaoJia
* @description 针对表【train_homework(培训系统-作业)】的数据库操作Service实现
* @createDate 2024-02-04 23:06:21
*/
@Service
public class TrainHomeworkServiceImpl extends ServiceImpl<TrainHomeworkMapper, TrainHomework>
    implements TrainHomeworkService{

    @Resource
    private TrainHomeworkMapper trainHomeworkMapper;

    private final static int DEFAULT_PAGE_NUMBER = 1;
    private final static int DEFAULT_PAGE_SIZE = 100;

    @Override
    public Page<TrainHomework> queryByParam(TrainHomeworkParam param) {
        int pageNumber = param.getPageNumber();
        int pageSize = param.getPageSize();
        pageNumber = pageNumber <= 0 ?  DEFAULT_PAGE_NUMBER : pageNumber;
        pageSize = pageSize > 100 ? DEFAULT_PAGE_SIZE : pageSize;
        QueryWrapper<TrainHomework> queryWrapper = new QueryWrapper<>();

        Integer status = CommonUtil.isNullId(param.getStatus()) ? HomeWorkStatus.ENABLE.getKey() : param.getStatus();
        queryWrapper.eq("status", status);
        if (!CommonUtil.isNullId(param.getId())){
            queryWrapper.eq("id", param.getId());
        }
        if (StringUtils.isNotEmpty(param.getTitle())){
            queryWrapper.eq("title", param.getTitle());
        }
        if (param.getCreateStartTime() != null) {
            queryWrapper.ge("create_time", param.getCreateEndTime());
            Date date = param.getCreateEndTime() == null ? new Date() : param.getCreateEndTime();
            queryWrapper.le("create_time", date);
        }
        if (!CommonUtil.isNullId(param.getCreatorId())){
            queryWrapper.eq("creator", param.getCreatorId());
        }
        if (!CommonUtil.isNullId(param.getCompanyId())){
            queryWrapper.eq("company_id", param.getCompanyId());
        }
        queryWrapper.orderByDesc(param.getOrderName());
        Page<TrainHomework> page = new Page<>(pageNumber, pageSize);
        return trainHomeworkMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Page<TrainHomework> queryByHwIdsAndPage(List<Long> homeworkIds, TrainClassUserParam param) {
        if (CollectionUtils.isEmpty(homeworkIds)){
            return null;
        }
        QueryWrapper<TrainHomework> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", homeworkIds).eq("status", HomeWorkStatus.ENABLE.getKey());
        if (CommonUtil.isNotNullId(param.getType())){
            Date date = new Date();
            if (HomeWorkTypeEnum.END.getStatus().equals(param.getType())){
                queryWrapper.le("end_time", date);
            }else if (HomeWorkTypeEnum.ING.getStatus().equals(param.getType())){
                queryWrapper.ge("end_time", date);
            }
        }
        Page<TrainHomework> page = new Page<>(param.getPageNumber(), param.getPageSize());
        return trainHomeworkMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<TrainHomework> queryByIds(List<Long> homeworkIds) {
        if (CollectionUtils.isEmpty(homeworkIds)){
            return null;
        }
        QueryWrapper<TrainHomework> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", homeworkIds).eq("status", HomeWorkStatus.ENABLE.getKey());
        return trainHomeworkMapper.selectList(queryWrapper);
    }

    @Override
    public Page<TrainHomework> getAssignedHomeWorkForClass(TrainClassUserParam param) {
        Long trainClassId = param.getTrainClassId();
        int offset = param.getOffset();
        int pageSize = param.getPageSize();
        Page<TrainHomework> result = new Page<>();
        long count = trainHomeworkMapper.getAssignedHomeworkCountForClass(trainClassId, HomeWorkStatus.ENABLE.getKey());
        result.setSize(pageSize);
        result.setCurrent(param.pageNumber);
        result.setTotal(count);
        if (count == 0) {
            return result;
        }
        List<TrainHomework> records = trainHomeworkMapper.getAssignedHomeworkForClass(trainClassId, HomeWorkStatus.ENABLE.getKey(),offset, pageSize);
        result.setRecords(records);
        return result;
    }


    @Override
    public List<TrainHomework> getAllAssignedHomeWorkForClass(Long classId) {
        return trainHomeworkMapper.getAllAssignedHomework(classId, HomeWorkStatus.ENABLE.getKey());
    }

    @Override
    public Page<TrainHomework> queryHomeWork(TrainClassUserParam param) {
        Long trainClassId = param.getTrainClassId();
        int offset = param.getOffset();
        int pageSize = param.getPageSize();
        Page<TrainHomework> result = new Page<>();
        result.setCurrent(param.getPageNumber());
        result.setSize(pageSize);
        Date date = new Date();
        List<TrainHomework> records = new ArrayList<>();
        long count = 0L;
        if (HomeWorkTypeEnum.END.getStatus().equals(param.getType())){
            count = trainHomeworkMapper.queryHomeWorkleCount(trainClassId, HomeWorkStatus.ENABLE.getKey(), date);
            records = trainHomeworkMapper.queryHomeWorkle(trainClassId, HomeWorkStatus.ENABLE.getKey(), date, offset, pageSize);
        }else if (HomeWorkTypeEnum.ING.getStatus().equals(param.getType())){
            count = trainHomeworkMapper.queryHomeWorkGeCount(trainClassId, HomeWorkStatus.ENABLE.getKey(), date);
            records  = trainHomeworkMapper.queryHomeWorkGe(trainClassId, HomeWorkStatus.ENABLE.getKey(), date, offset, pageSize);
        }else {
            count = trainHomeworkMapper.queryHomeWorkCount(trainClassId, HomeWorkStatus.ENABLE.getKey());
            records  = trainHomeworkMapper.queryHomeWork(trainClassId, HomeWorkStatus.ENABLE.getKey(), offset, pageSize);
        }
        result.setTotal(count);
        result.setRecords(records);
        return result;
    }


}




